<template>
  <div id="Support">
    <div style="height: 30px">
      <a-button type="primary" @click="addbtn" style="float: right"
        >新增</a-button
      >
    </div>
    <a-table
      :scroll="{ y: 580 }"
      :columns="columns"
      style="margin-top: 16px"
      :data-source="tableData"
      :pagination="tablePagination"
      :row-selection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
      }"
      :rowKey="(record, index) => index"
      @change="handleTableChange"
    >
      <template slot="name" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.name">
          <template slot="title">
            <span>{{ record.name }}</span>
          </template>
          <span style="cursor: default">{{ record.name }}</span>
        </a-tooltip>
        <span v-else-if="!record.name">—</span>
      </template>
      <template slot="tapdProjectId" slot-scope="text, record">
        <a-tooltip placement="top" v-if="record.tapdProjectId">
          <template slot="title">
            <p>{{ record.tapdProjectId }}</p>
          </template>
          <span style="cursor: default">{{ record.tapdProjectId }}</span>
        </a-tooltip>
        <span v-else-if="!record.tapdProjectId">—</span>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-button
          v-if="record.operation"
          type="primary"
          @click="editClick(record)"
          style="margin-right: 10px"
          >修改</a-button
        >
        <a-button
          v-if="record.operation"
          type="danger"
          @click="deleteClick(record)"
          >删除</a-button
        >
      </template>
    </a-table>
    <a-modal
      v-model="visible"
      :title="title"
      :maskClosable="false"
      @cancel="handleCancel"
    >
      <a-form-model
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 12 }"
        ref="formRef"
        :model="form"
        :rules="rules"
      >
        <a-form-model-item label="维护保障名称" prop="name">
          <a-input v-model="form.name" />
        </a-form-model-item>
        <a-form-model-item label="维护保障人员" prop="personnel">
          <a-input v-model="form.personnel" />
        </a-form-model-item>
        <a-form-model-item label="维护保障时间" prop="time">
          <a-date-picker
            v-model="form.time"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            style="width: 236px"
          />
        </a-form-model-item>
        <a-form-model-item label="维护保障内容" prop="content">
          <a-input v-model="form.content" />
        </a-form-model-item>
      </a-form-model>
      <template slot="footer">
        <a-button key="back" class="modalCancel" @click="handleCancel"
          >取消</a-button
        >
        <a-button key="submit" @click="primaryClick" type="primary">
          确定
        </a-button>
      </template>
    </a-modal>
  </div>
</template>
    
<script>
import moment from "moment";

export default {
  data() {
    return {
      form: {
        id: null,
        name: "", // 维护保障名称
        personnel: "", //维护保障人员
        time: "", //维护保障时间
        content: "", //维护保障内容
      },
      rules: {
        name: [
          { required: true, message: "请输入维护保障名称", trigger: "blur" },
        ],
        personnel: [
          { required: true, message: "请输入维护保障人员", trigger: "blur" },
        ],
        time: [
          { required: true, message: "请输入维护保障时间", trigger: "change" },
        ],
        content: [
          { required: true, message: "请输入维护保障内容", trigger: "blur" },
        ],
      },
      visible: false,
      title: null,
      selectedRowKeys: [],
      columns: [
        //主页table列的配置
        {
          title: "维护保障名称",
          dataIndex: "name",
          customRender: (text, record) => {
            return record.id ? record.id : "—";
          },
        },
        {
          title: "维护保障人员",
          dataIndex: "personnel",
          customRender: (text, record) => {
            return record.personnel ? record.personnel : "—";
          },
        },
        {
          title: "维护保障时间",
          dataIndex: "time",
          customRender: (text, record) => {
            return record.time ? record.time : "—";
          },
        },
        {
          title: "维护保障内容",
          dataIndex: "content",
          customRender: (text, record) => {
            return record.content ? record.content : "—";
          },
        },
        {
          title: "操作",
          width: 180,
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
      tableData: [
        //主页表格的数据
      ],
      tablePagination: {
        //主页表格的分页器
        total: 0,
        current: 1,
        pageSize: 10,
        showQuickJumper: true, //是否可以快速跳转至某页
        showSizeChanger: true, //是否可以改变 pageSize
      },
    };
  },
  created() {
    this.getData(); //获取列表数据
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.tablePagination.current = pagination.current;
      this.tablePagination.pageSize = pagination.pageSize;
      this.getData();
    },
    deleteClick(row) {
      //点击删除
      let data = {
        id: row.id,
      };
      let that = this;
      this.$confirm({
        title: "确认删除?",
        content: "您确定要删除此项吗?",
        onOk() {
          that.$axios.delete("/maintain/delete", { data }).then((res) => {
            if (res.code == 200) {
              that.$message.success("删除成功");
              that.getData();
            }
          });
        },
        onCancel() {
          console.log("取消删除");
        },
      });
    },
    async getData() {
      let { data } = await this.$axios.get("/maintain/page/list", {
        params: {
          page: this.tablePagination.current,
          size: this.tablePagination.pageSize,
        },
      });
      this.tableData = data.records;
      this.tablePagination.total = data.total;
    },
    onSelectChange(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    },
    editClick(row) {
      //编辑
      this.$axios
        .get("/maintain/detail", {
          params: {
            id: row.id,
          },
        })
        .then((res) => {
          if (res.code == 200) {
            this.form = res.data;
            this.form.id = row.id;
            this.title = "编辑";
            this.visible = true;
          }
        });
    },
    addbtn() {
      //新增
      for (let i in this.form) {
        this.form[i] = null;
      }
      this.title = "新增";
      this.visible = true;
    },
    handleCancel() {
      this.form = {
        id: null,
        name: "", // 维护保障名称
        personnel: "", //维护保障人员
        time: "", //维护保障时间
        content: "", //维护保障内容
      };
      this.visible = false;
    },
    primaryClick() {
      this.$nextTick(() => {
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            let data = {
              id: this.form.id,
              name: this.form.name, // 维护保障名称
              personnel: this.form.personnel, //维护保障人员
              time: this.form.time, //维护保障时间
              content: this.form.content, //维护保障内容
            };
            if (this.title == "新增") {
              this.$axios.post("/maintain/create", data).then((res) => {
                if (res.code == 200) {
                  this.$message.success("新增成功");
                  this.getData();
                  this.handleCancel();
                }
              });
            } else if (this.title == "编辑") {
              this.$axios.put("/maintain/update", data).then((res) => {
                if (res.code == 200) {
                  this.$message.success("编辑成功");
                  this.getData();
                  this.handleCancel();
                }
              });
            }
          } else {
            console.log("校验失败");
          }
        });
      });
    },
  },
};
</script>
    
<style>
</style>